//
//  ZYSubAccountViewCell.m
//  YiLin
//
//  Created by Zyark on 2018/6/1.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYSubAccountViewCell.h"
#import "UIButton+ImageTitleSpacing.h"
#import "ZYUser.h"

@interface ZYSubAccountViewCell()
@property(nonatomic,strong) UILabel *accountNameLabel;
@property(nonatomic,strong) UIButton *editBtn;
@property(nonatomic,strong) UIButton *deleteBtn;
@property(nonatomic,strong) ZYChangeLanguage *language;
@end

@implementation ZYSubAccountViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        _language = [ZYChangeLanguage sharedZYChangeLanguage];
        [self setupView];
    }
    return self;
}

-(void)setupView{
    UILabel *accountNameLabel = [[UILabel alloc] init];
    accountNameLabel.textColor = kLabelColor;
    accountNameLabel.textAlignment = NSTextAlignmentLeft;
    accountNameLabel.font = [UIFont systemFontOfSize:18];
    UIButton *editBtn = [self setupButton:[_language getStringByKey:LC_EDIT_BUTTON_TITLE] imageName:@"ic_sub_account_edit_btn"];
    [editBtn addTarget:self action:@selector(doEdit) forControlEvents:UIControlEventTouchUpInside];
    UIButton *deleteBtn = [self setupButton:[_language getStringByKey:LC_DELETE_BUTTON_TITLE] imageName:@"ic_sub_account_delete_btn"];
    [deleteBtn addTarget:self action:@selector(doDelete) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:accountNameLabel];
    [self addSubview:editBtn];
    [self addSubview:deleteBtn];
    
    ZYWeakSelf(weakSelf)
    CGFloat width = 80;
    CGFloat height = 25;
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_top);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(height);
    }];
    
    [editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(deleteBtn.mas_centerY);
        make.right.equalTo(deleteBtn.mas_left).offset(-5);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(height);
    }];
    
    [accountNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(editBtn.mas_centerY);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.right.equalTo(editBtn.mas_left).offset(-15);
    }];
    
    _accountNameLabel = accountNameLabel;
    _editBtn = editBtn;
    _deleteBtn = deleteBtn;
}

-(UIButton *)setupButton:(NSString *)title imageName:(NSString *)imageName{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.size = CGSizeMake(25, 25);
    [button setTitle:title forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [button setTitleColor:RGBA(51, 51, 51, 1) forState:UIControlStateNormal];
    [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:5];
//    [button setImageEdgeInsets:UIEdgeInsetsMake(5,5,-5,-5)];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    return button;
}

-(void)setAccount:(ZYUser *)account{
    _account = account;
    _accountNameLabel.text = account.username;
}

-(void)doDelete{
    if([_delegate respondsToSelector:@selector(subAccountViewCell:doDelete:)]){
        [_delegate subAccountViewCell:self doDelete:_account];
    }
}

-(void)doEdit{
    if([_delegate respondsToSelector:@selector(subAccountViewCell:doEdit:)]){
        [_delegate subAccountViewCell:self doEdit:_account];
    }
}
@end
